package com.saxing.testflux.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.saxing.testflux.R;
import com.saxing.testflux.action.CommonAction;
import com.saxing.testflux.action.CommonActionCreator;
import com.saxing.testflux.store.SecondStore;
import com.saxing.testflux.store.Store;
import com.saxing.testflux.view.base.BaseFluxActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by saxing on 17-7-9.
 */

public class SecondActivity extends BaseFluxActivity {

    @BindView(R.id.btn_refresh) Button btn_refresh;
    @BindView(R.id.progressbar) ProgressBar progressBar;

    private CommonActionCreator commonActionCreator;
    SecondStore secondStore;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initDependencies();
    }

    private void initDependencies() {
        commonActionCreator = new CommonActionCreator(dispatcher);
        secondStore = new SecondStore();
        dispatcher.register(this, secondStore);
    }

    @OnClick(R.id.btn_refresh) void onRefresh(){
        btn_refresh.setText("refresh...");
        progressBar.setVisibility(View.VISIBLE);
        commonActionCreator.refreshData();
    }

    @OnClick(R.id.btn_delete) void onDelete(){
        commonActionCreator.deleteData();
    }

    @Override
    public void onViewUpdate(Object event) {
        if (event instanceof Store.StoreChangeEvent){
            SecondStore.SecondStoreEvent secondStoreEvent = (SecondStore.SecondStoreEvent) event;
            if (secondStoreEvent.getOperationType().equals(CommonAction.TYPE_REFRESH)){
                btn_refresh.setText("refresh complete");
                progressBar.setVisibility(View.GONE);
            }else if (secondStoreEvent.getOperationType().equals(CommonAction.TYPE_DELETE)){
                Toast.makeText(this, "Delete...", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public Store initStore() {
        return secondStore;
    }
}
